﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Zhaoxi.CourseManagement.Common;
using Zhaoxi.CourseManagement.DataAccess;
using Zhaoxi.CourseManagement.DataAccess.DataEntity;
using Zhaoxi.CourseManagement.Model;
using Zhaoxi.CourseManagement.MysqlCommon;

namespace Zhaoxi.CourseManagement.ViewModel
{
    /**
     * 登录逻辑类
     */
    public class LoginViewModel : NotifyBase
    {
        public CommandBase CloseWindowCommand { get; set; }

        //初始化
        public LoginModel LoginModel { get; set; } = new LoginModel();

        public CommandBase LoginCommand { get; set; }

        //返回错误信息
        private string _errorMessage;

        //点击登录时的进度条
        private Visibility _showProgress = Visibility.Collapsed;

        public string ErrorMessage
        {
            get { return _errorMessage; }
            set 
            { 
                _errorMessage = value; 
                this.DoNotify();
            }
        }

        public Visibility ShowProgress
        {
            get { return _showProgress; }
            set
            {
                _showProgress = value; 
                this.DoNotify();
                LoginCommand.RaiseCanExecuteChanged();
            }
        }

        public LoginViewModel()
        {
            //this.LoginModel.ValidationCode = "ETU4";
            //初始化
            this.CloseWindowCommand = new CommandBase();
            this.CloseWindowCommand.DoExecute = new Action<object>((o) =>
            {
                (o as Window).Close();
            });
            this.CloseWindowCommand.DoCanExecute = new Func<object, bool>((o) => { return true; });

            this.LoginCommand = new CommandBase();
            this.LoginCommand.DoExecute = new Action<object>(DoLogin);
            this.LoginCommand.DoCanExecute = new Func<object, bool>((o) => 
            { 
                return ShowProgress == Visibility.Collapsed; 
            });
        }

        private void DoLogin(object o)
        {
            this.ShowProgress = Visibility.Visible;
            this.ErrorMessage = "";
            //对用户名进行验证
            if (string.IsNullOrEmpty(LoginModel.UserName))
            {
                this.ErrorMessage = "请输入用户名！";
                this.ShowProgress = Visibility.Collapsed;
                return;
            }
            if (string.IsNullOrEmpty(LoginModel.Password))
            {
                this.ErrorMessage = "请输入密码！";
                this.ShowProgress = Visibility.Collapsed;
                return;
            }
            if (string.IsNullOrEmpty(LoginModel.ValidationCode))
            {
                this.ErrorMessage = "请输入验证码！";
                this.ShowProgress = Visibility.Collapsed;
                return;
            }
            if (LoginModel.ValidationCode.ToLower() != "etu4")
            {
                this.ErrorMessage = "验证码输入不正确！";
                this.ShowProgress = Visibility.Collapsed;
                return;
            }

            //关闭当前登录界面（显示主界面逻辑在app.xaml里）
            //Task.Run(new Action(() =>
            //{
            try
            {
                //查询数据库对用户名跟密码进行查询后判断
                //UserEntity userInfo = LocalDataAccess.GetInstance().CheckUserInfo(LoginModel.UserName, LoginModel.Password);
                //MysqlConnect mysqlConnect = new MysqlConnect();
                //UserEntity userInfo = mysqlConnect.ToConnection(LoginModel.UserName, LoginModel.Password);

                /*                if (userInfo == null)
                                {
                                    this.ErrorMessage = "用户名或密码错误！";
                                    this.ShowProgress = Visibility.Collapsed;
                                    return;
                                }*/
                UserEntity userInfo = new UserEntity();
                userInfo.Avatar = "../123";
                userInfo.UserName = LoginModel.UserName;
                userInfo.Password = LoginModel.Password;
                userInfo.Gender = 1;
                GlobalValues.UserInfo = userInfo;

                //Thread.Sleep(1000); // 2000毫秒等于2秒
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    (o as Window).DialogResult = true;
                }));
            }
            catch (Exception e)
            {
                this.ErrorMessage = e.Message;
            }
            finally
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    this.ShowProgress = Visibility.Collapsed;
                }));
            }
            //}));
            
        }
    }
}
